// pages/wb/help.js
var common = require('../../utils/mr_xu/common.js')
var api = require("../../utils/mr_xu/api_url.js").api
const app = getApp()
var post = {};
const dayjs = require('../../vendor/dayjs.min.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        index: 0,
        times: [],
        date: [
            '15', '30'
        ],
        pd: [],
    },
    bindPickerChange(e) {
        const {
            index
        } = e.target.dataset;
        const {
            value
        } = e.detail;

        this.setData({
            [`pd[${index}].type`]: this.data.date[+value],
            [`pd[${index}].pickerValue`]: value,
        })
    },
    submit(e) {
        const {
            value
        } = e.detail;

        if (!value.stock) {
            return wx.showToast({
                title: '请设置接单数量',
                icon: 'none'
            })
        }
        if (!value.price) {
            return wx.showToast({
                title: '请设置接单价格',
                icon: 'none'
            })
        }
        value.type = this.data.date[+value.type];
        const data = this.data.pd[e.target.dataset.index];
        data.beginAt = data.date
        app.http.post(`/rest/plate-number/${data.id?'update':'create'}`, {
            data: Object.assign(data, value),
        }).then(res => {
            wx.showToast({
                title: '保存成功',
            })
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        const date = dayjs();

        this.setData({
            pd: [{
                    displayTime: date.format('MM.DD'),
                    date: date.format('YYYY-MM-DD'),
                    type: 15,
                },
                {
                    displayTime: date.add(1, 'day').format('MM.DD'),
                    date: date.add(1, 'day').format('YYYY-MM-DD'),
                    type: 15,
                },
            ]
        })
        getApp().http.post('/rest/plate-number/list-2days').then(res => {
            for (let i = 0; i < 2; i++) {
                const d = res.content[i];
                d.pickerValue = this.data.date.findIndex(o => o == d.type);
                d && this.setData({
                    [`pd[${i}]`]: Object.assign(this.data.pd[i], d),
                })
            }
        })
    },
})